package com.service.calculate.vehicle;

import com.service.bean.device.Vehicle;
import com.service.bean.signal.GpsSignal;
import com.service.bean.signal.GpsSignalWork;
import com.service.tools.DateFormat;

/**
 * 上下两条数据的变化量计算
 * 
 * @author lj Created on 2015年6月5日
 */
public class ChangeCalculator {
	/**
	 * 当前的Gps信号
	 */
	private GpsSignal gpsSignal;

	private Vehicle vehicle;

	public ChangeCalculator(GpsSignal gpsSignal, Vehicle vehicle) {
		this.gpsSignal = gpsSignal;
		this.vehicle = vehicle;
	}

	public GpsSignalWork calculator() {
		GpsSignalWork work = null;
		if (vehicle.lastGps != null) {
			synchronized (vehicle.lastGps) {
				work = new GpsSignalWork();
				work.signalId = gpsSignal.signalId;
				work.gpsTime = gpsSignal.gpsTime;
				work.equipmentId = gpsSignal.equipmentId;
				work.realOilCal = gpsSignal.oilAmtCal;
				// 前后两条数据的时间差，单位秒
				long misTiming = (gpsSignal.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime()) / 1000;
				// 前后两条数据的Gps里程差
				double misDistance = gpsSignal.totalMile - vehicle.lastGps.totalMile;
				// 前后两条数据的脉冲里程差
				double misDistance2 = gpsSignal.totalMile2 - vehicle.lastGps.totalMile2;
				// 前后两条数据的油位差
				double misOilAmt = vehicle.lastGps.oilAmtCal - gpsSignal.oilAmtCal ;
				if (1 == gpsSignal.isInWork) {
					work.workMile = misDistance;
					work.workMile2 = misDistance2;
					work.workTime = misTiming;
					work.workOilChange = misOilAmt;
					if (0 == gpsSignal.speed) {
						work.workStopTime = misTiming;
					}
				} else {
					work.noWorkTime = misTiming;
					work.noWorkMile = misDistance;
					work.noWorkMile2 = misDistance2;
					work.noWorkOilChange = misOilAmt;
					if (0 == gpsSignal.speed) {
						work.noWorkStopTime = misTiming;
					}
				}
				if (gpsSignal.speed != 0 && gpsSignal.accStatus == 1) {
					work.driveTime = misTiming;
				} else {
					work.stopTime = misTiming;
				}
				if( gpsSignal.areaId != null){
					work.isInArea = 1;
					work.areaId = gpsSignal.areaId;
					if(gpsSignal.areaId.equals(vehicle.lastGps.areaId) && gpsSignal.isInWork==1){
						work.areaKeepTime = (gpsSignal.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime())/1000 ;
						work.areaKeepMile = gpsSignal.totalMile - vehicle.lastGps.totalMile ;
					}else{
						work.areaKeepTime = 0 ;
						work.areaKeepMile = 0 ;
					}
					if(gpsSignal.isInWork==1){
						if(vehicle.lastGps.areaId != null){
							work.allAreaKeepTime = (gpsSignal.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime())/1000 ;
							work.allAreaKeepMile = gpsSignal.totalMile - vehicle.lastGps.totalMile ;
						}else{
							work.outAreaKeepTime = (gpsSignal.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime())/1000 ;
							work.outAllAreaKeepMile = gpsSignal.totalMile - vehicle.lastGps.totalMile ;
						}
					}
					
				}else{
					work.isInArea = 0 ;
				}
				if(gpsSignal.accStatus == 1){
					// 如果最后一条定位数据时间与当前GPS时间是同一天
					if (vehicle.lastGps.gpsTime != null && DateFormat.format(vehicle.lastGps.gpsTime, "yyyy-MM-dd").equals(DateFormat.format(gpsSignal.gpsTime, "yyyy-MM-dd"))) {
						work.outWorkTime = (gpsSignal.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime())/1000 ;
					}else {// 如果最后一条定位数据时间与当前GPS时间不是同一天(例如：今天的第一条GPS)
						work.outWorkTime = 0;
						work.outAreaKeepTime=0;
					}
					work.outWorkMile = gpsSignal.totalMile - vehicle.lastGps.totalMile ;
				}
				
				//油位变化量
				work.oilChange = work.workOilChange+work.noWorkOilChange;
				//里程变化量
				work.mileChange = work.workMile+work.noWorkMile;
				//停车时长的累计
				work.sumStopTime = work.stopTime == 0?0:vehicle.lastGps.sumStopTime + work.stopTime;
				//工作状态中，速度为0的时长的累计
				work.sumWorkStopTime = work.workStopTime == 0?0:vehicle.lastGps.sumWorkStopTime + work.workStopTime;		
			}
		}
		return work;
	}

}
